// 平台级用户和权限类型定义

// 平台用户类型
export interface PlatformUser {
  id: string;
  username: string;
  email: string;
  realName: string;
  avatar?: string;
  phone?: string;
  
  // 简化的权限模型
  isSuperAdmin: boolean;               // 是否为超级管理员
  roleIds?: string;                    // 角色ID列表，用逗号分隔
  
  // 权限和菜单信息
  roleCodes?: string[];                // 角色编码列表
  permissions?: string[];              // 权限列表
  menuCodes?: string[];                // 菜单编码列表
  
  // 租户访问控制
  tenantAccessType?: 'all' | 'specific' | 'none'; // 租户访问类型
  accessibleTenantIds?: string;        // 可访问的租户ID列表，用逗号分隔（当accessType为specific时使用）
  accessibleTenants?: string[];        // 兼容性字段，将从accessibleTenantIds解析
  
  // 状态信息
  status: 'active' | 'inactive' | 'locked';
  lastLoginTime?: string;
  lastLoginIp?: string;
  loginCount?: number;
  createTime: string;
  updateTime: string;
  createdBy: string;
  
  // 向后兼容字段
  level?: 'platform' | 'tenant';       // 用户级别（兼容性）
  platformRoles?: string[];            // 平台角色（兼容性）
}

// 平台角色类型
export interface PlatformRole {
  id: string;
  code: string;
  name: string;
  description?: string;
  permissions: string[];               // 权限列表
  scope: 'global' | 'tenant' | 'limited'; // 权限范围
  sort?: number;                       // 排序字段
  
  status: 'active' | 'inactive' | number; // 支持字符串和数字状态：1-启用，0-禁用
  isBuiltin: boolean;                  // 是否为内置角色
  createTime: string;
  updateTime: string;
  createdBy: string;
}

// 平台权限类型
export interface PlatformPermission {
  id: string;
  code: string;
  name: string;
  description?: string;
  category: string;                    // 权限分类
  
  // 权限控制
  dangerous: boolean;                  // 是否为危险操作
  requireConfirm: boolean;             // 是否需要二次确认
  
  createTime: string;
  updateTime: string;
}

// 平台级登录请求
export interface PlatformLoginRequest {
  username: string;
  password: string;
  captcha?: string;
  rememberMe?: boolean;
}

// 平台级登录响应
export interface PlatformLoginResponse {
  token?: string;                      // 兼容性字段
  accessToken: string;                 // 后端实际返回的字段
  user: PlatformUser;
  permissions: string[];
  accessibleTenants?: string[];        // 可访问的租户ID列表
}

// 租户管理相关类型
export interface TenantManagement {
  id: string;
  code: string;
  name: string;
  domain?: string;
  logo?: string;
  description?: string;
  industry?: string;
  scale: 'startup' | 'sme' | 'enterprise' | 'large';
  
  // 管理信息
  adminUser: {
    name: string;
    email: string;
    phone?: string;
  };
  
  // 状态和统计
  status: 'active' | 'inactive' | 'suspended' | 'expired';
  userCount: number;
  storageUsed: number;                 // GB
  storageLimit: number;                // GB
  lastActiveTime?: string;
  
  // 订阅信息
  subscription: {
    plan: 'free' | 'basic' | 'professional' | 'enterprise';
    status: 'active' | 'trial' | 'overdue' | 'cancelled';
    startDate: string;
    endDate?: string;
    price: number;
    currency: string;
  };
  
  createTime: string;
  updateTime: string;
  createdBy: string;
}

// 平台统计信息
export interface PlatformStats {
  totalTenants: number;
  activeTenants: number;
  totalUsers: number;
  activeUsers: number;
  totalStorage: number;                // GB
  usedStorage: number;                 // GB
  
  // 最近数据
  recentTenants: TenantManagement[];
  recentLogins: Array<{
    userId: string;
    userName: string;
    tenantId?: string;
    tenantName?: string;
    loginTime: string;
    ip: string;
  }>;
  
  // 收入统计
  revenue: {
    thisMonth: number;
    lastMonth: number;
    thisYear: number;
    currency: string;
  };
}

// 租户操作日志
export interface TenantOperationLog {
  id: string;
  tenantId: string;
  tenantName: string;
  operatorId: string;
  operatorName: string;
  operationType: 'create' | 'update' | 'delete' | 'suspend' | 'activate' | 'config';
  operationDesc: string;
  details?: Record<string, any>;
  
  result: 'success' | 'failure';
  errorMessage?: string;
  
  ip: string;
  userAgent?: string;
  createTime: string;
}

// ==================== 新增类型定义 ====================

// 操作日志类型
export interface OperationLog {
  id: string;
  operatorId: string;
  operatorName: string;
  operatorType: 'platform_admin' | 'tenant_admin' | 'system';
  tenantId?: string;
  tenantName?: string;
  action: string;
  target: string;
  targetId: string;
  description: string;
  result: 'success' | 'failure' | 'partial';
  ip: string;
  userAgent: string;
  timestamp: string;
  details?: Record<string, any>;
}

// 系统监控指标
export interface SystemMetrics {
  id: string;
  metricType: 'cpu' | 'memory' | 'disk' | 'network';
  value: number;
  unit: string;
  timestamp: string;
  threshold?: {
    warning: number;
    critical: number;
  };
}

// 服务状态
export interface ServiceStatus {
  id: string;
  name: string;
  displayName: string;
  status: 'running' | 'stopped' | 'error' | 'warning';
  version?: string;
  uptime?: number;
  lastCheck: string;
  endpoint?: string;
  description?: string;
}

// 系统告警
export interface SystemAlert {
  id: string;
  level: 'info' | 'warning' | 'critical';
  title: string;
  message: string;
  source: string;
  status: 'active' | 'resolved' | 'acknowledged';
  createTime: string;
  resolveTime?: string;
  acknowledgedBy?: string;
  acknowledgedTime?: string;
}

// 工单类型
export interface PlatformTicket {
  id: string;
  title: string;
  description: string;
  type: 'technical' | 'business' | 'billing' | 'other';
  priority: 'low' | 'medium' | 'high' | 'urgent';
  status: 'open' | 'in_progress' | 'resolved' | 'closed';
  
  // 提交者信息
  submitterId: string;
  submitterName: string;
  submitterEmail: string;
  tenantId?: string;
  tenantName?: string;
  
  // 处理者信息
  assigneeId?: string;
  assigneeName?: string;
  
  // 时间信息
  createTime: string;
  updateTime: string;
  resolveTime?: string;
  closeTime?: string;
  
  // 附加信息
  attachments?: Array<{
    id: string;
    name: string;
    url: string;
    size: number;
  }>;
  
  // 处理记录
  timeline: TicketTimelineItem[];
}

// 工单时间线项目
export interface TicketTimelineItem {
  id: string;
  type: 'create' | 'assign' | 'comment' | 'status_change' | 'resolve' | 'close';
  content: string;
  operatorId: string;
  operatorName: string;
  timestamp: string;
  details?: Record<string, any>;
}

// 平台用户管理
export interface PlatformUserManagement {
  id: string;
  username: string;
  email: string;
  realName: string;
  avatar?: string;
  phone?: string;
  
  // 平台级权限
  platformRoles: string[];
  permissions: string[];
  accessibleTenants: string[];
  
  // 状态信息
  status: 'active' | 'inactive' | 'locked';
  isSuperAdmin: boolean;
  lastLoginTime?: string;
  lastLoginIp?: string;
  loginCount: number;
  
  // 安全信息
  twoFactorEnabled: boolean;
  passwordLastChanged?: string;
  
  createTime: string;
  updateTime: string;
  createdBy: string;
}

// 监控统计
export interface MonitorStats {
  system: {
    cpu: SystemMetrics;
    memory: SystemMetrics;
    disk: SystemMetrics;
    network: SystemMetrics;
  };
  services: ServiceStatus[];
  alerts: SystemAlert[];
  performance: {
    avgResponseTime: number;
    requestsPerSecond: number;
    errorRate: number;
    uptime: number;
  };
}

// API查询参数类型
export interface LogQueryParams {
  page?: number;
  size?: number;
  action?: string;
  result?: string;
  operatorType?: string;
  tenantId?: string;
  startDate?: string;
  endDate?: string;
  search?: string;
}

export interface TicketQueryParams {
  page?: number;
  size?: number;
  status?: string;
  type?: string;
  priority?: string;
  assigneeId?: string;
  tenantId?: string;
  search?: string;
}

export interface UserQueryParams {
  page?: number;
  size?: number;
  status?: string;
  role?: string;
  search?: string;
}

// ==================== 租户管理类型定义 ====================

// 平台租户DTO类型
export interface PlatformTenantDto {
  id?: number;
  code: string;
  name: string;
  domain?: string;
  logo?: string;
  description?: string;
  contactName?: string;
  contactEmail?: string;
  contactPhone?: string;
  address?: string;
  status?: number; // 1-正常，2-试用，3-暂停，4-过期，0-禁用
  planType?: string; // basic-基础版，pro-专业版，enterprise-企业版
  maxUsers?: number;
  currentUsers?: number;
  expireTime?: string;
  databaseName?: string;
  databaseConfig?: string;
  featureConfig?: string;
  settings?: string;
  metadata?: string;
  userUsageRate?: number;
  isExpiringSoon?: boolean;
  createTime?: string;
  updateTime?: string;
  createdBy?: string;
  updatedBy?: string;
}

// 平台租户查询类型
export interface PlatformTenantQuery {
  current?: number;
  size?: number;
  code?: string;
  name?: string;
  domain?: string;
  contactName?: string;
  contactEmail?: string;
  status?: number;
  planType?: string;
  isExpiringSoon?: boolean;
  minUsers?: number;
  maxUsers?: number;
  startDate?: string;
  endDate?: string;
}